package com.study.bean;

import lombok.Data;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @desc:
 * @author: panxiyi
 * @date: 2023/5/20 10:03 下午
 */
//@Component
//@ConfigurationProperties(prefix = "cartoon")
@Data
@EnableConfigurationProperties(CartoonProperties.class)
// 使用@EnableConfigurationProperties注解设定使用属性类时加载bean
public class CartoonCatAndMouse implements ApplicationContextAware {

    private Cat cat;
    private Mouse mouse;


    private CartoonProperties cartoonProperties;

    CartoonCatAndMouse(CartoonProperties cartoonProperties) {
        this.cartoonProperties = cartoonProperties;
        cat = new Cat();
        cat.setName(cartoonProperties.getCat() != null && StringUtils.hasText(cartoonProperties.getCat().getName()) ? cartoonProperties.getCat().getName() : "tom");
        cat.setAge(cartoonProperties.getCat() != null && cartoonProperties.getCat().getName() != null ? cartoonProperties.getCat().getAge() : 4);
        mouse = new Mouse();
        mouse.setName(cartoonProperties.getMouse() != null && StringUtils.hasText(cartoonProperties.getMouse().getName()) ? cartoonProperties.getMouse().getName() : "jerry");
        mouse.setAge(cartoonProperties.getMouse() != null && cartoonProperties.getMouse().getName() != null ? cartoonProperties.getMouse().getAge() : 5);
    }


    public void play() {
        String[] beans = applicationContext.getBeanDefinitionNames();
        for (String bean : beans) {
            System.out.println(bean);
        }
        System.out.println(cat.getAge() + "岁的" + cat.getName() + " 和" + mouse.getAge() +"岁的" + mouse.getName() +"打起来了");
    }

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
